package com.example.demo.juc.lock;

import lombok.SneakyThrows;

import java.util.concurrent.TimeUnit;

/**
 * @Author: xiatian
 * @CreateTime: 2022/2/27 9:35 下午
 * @Description:
 */
public class DeadLockDemo implements Runnable {

    private String LockA;
    private String LockB;

    public DeadLockDemo(String lockA, String lockB) {
        LockA = lockA;
        LockB = lockB;
    }

    @SneakyThrows
    @Override
    public void run() {
        synchronized (LockA) {
            System.out.println(Thread.currentThread().getName() + " 线程持有" + LockA + "尝试获取" + LockB);
            TimeUnit.MILLISECONDS.sleep(1);
            synchronized (LockB) {
                System.out.println(Thread.currentThread().getName() + " 线程获取锁" + LockB);
            }
        }
    }


    public static void main(String[] args) {
        String LockA = "LockA";
        String LockB = "LockB";
        new Thread(new DeadLockDemo(LockA, LockB), "AA").start();
        new Thread(new DeadLockDemo(LockB, LockA), "BB").start();
    }
}
